<?php
namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use App\Model\AuthGroup;
use App\Model\AuthRule;
use EasySwoole\Validate\Validate;

class Authmanager extends AdminBase
{

    /**
     * 权限组
     * @return AuthGroup
     */
    public function getAuthGroupModel()
    {
        return new AuthGroup();
    }

    /**
     * 规则组
     * @return AuthRule
     */
    public function getAuthRuleModel()
    {
        return new AuthRule();
    }

    /**
     * 菜单
     * @return \App\Model\Menu
     */
    public function getMenuModel()
    {
        return new \App\Model\Menu();
    }

    /**
     * 权限组首页
     * @return bool|string|void
     */
    public function index()
    {
        if ($this->isPost()) {
            return $this->writePageJson('操作成功', $this->getAuthGroupModel()->getAuthGroupList($this->postParams(), []));
        }
        return $this->fetch();
    }

    /**
     * 新增权限组
     * @return string|void
     */
    public function add()
    {
        if ($this->isPost()) {
            return $this->writeJson('添加成功',$this->getAuthGroupModel()->updateOrCreateData($this->postParams(),['id'=>$this->postParam('id')]));
        }
        return $this->fetch();
    }


    /**
     * 权限组编辑
     * @return string|void
     */
    public function edit()
    {
        if ($this->isPost()) {
            return $this->writeJson('编辑成功',$this->getAuthGroupModel()->updateOrCreateData($this->postParams(),['id'=>$this->postParam('id')]));
        }
        $id = $this->requestParam('id');
        $info = $this->getAuthGroupModel()->getAuthGroupInfo(['id' => $id]);
        $this->assign(['info'=>$info]);
        return $this->fetch();
    }
    /**
     * 删除权限组
     * @return mixed
     */
    public function del(){
        $id=$this->requestParam('id');
        if ($this->isPost()) {
            return $this->writeJson('删除成功',$this->getAuthGroupModel()->delData(["id"=>$id]));
        }
    }


    /**
     * 菜单
     * @param Request $request
     * @return string|void
     */
    public function menu()
    {
        $id = $this->requestParam('id');
        if ($this->isPost()) {
            $data = $this->postParam('data');
            if (empty($data)) {
                $data = array();
            }
            return $this->writePageJson('操作成功', $this->getAuthGroupModel()->updateAuthGroup(["id" => $id], ["rules" => implode(",", $data)]));
        }
        $data = array_merge(['module' => $this->getModule(), 'action' => $this->getActionController(), 'mothod' => $this->getActionMethod()], $this->requestParams());
        $this->getAuthRuleModel()->updateNodeByMenu($data);  //更新权限菜单表

        $menuList = $this->getMenuModel()->getMenuList([]); //获取菜单

        //获取id 权限组
        $groupInfo = $this->getAuthGroupModel()->getAuthGroupInfo(["id" => $id]);

        //权限组则
        $own_rose_rule_arr = array();
        if (!empty($groupInfo['rules'])) {
            $own_rose_rule_arr = explode(",", $groupInfo['rules']);
        }

        //获取权限组菜单列表
        $ruleList = $this->getAuthRuleModel()->getAuthRuleListNameByArray(["module" => 'admin']);
        //生成新数组
        $new_menus = array();
        foreach ($menuList as $k => $item) {
            $new_row = array();
            if ($item['pid'] == 0) {
                $new_row['name'] = '顶级节点';
            }
            $new_row["id"] = $item['id'];
            $new_row["name"] = $item['title'];
            $new_row["pId"] = $item['pid'];
            $new_row["icon"] = $item['icon'];
            //判断对应的后台id
            $f_url = strtolower($data['module'] . '/' . $item['url']);
            $new_row["rule_id"] = $ruleList[$f_url];
            if (in_array($new_row["rule_id"], $own_rose_rule_arr)) {
                $new_row["checked"] = true;
            }
            $new_menus[] = $new_row;
            //合并起权限组菜单
        }
        if (!empty($own_rose_rule_arr)) {
            $new_menus[] = array(
                'id' => 0,
                'rule_id' => 0,
                'name' => '顶级权限',
                'checked' => true
            );
        } else {
            $new_menus[] = array(
                'id' => 0,
                'rule_id' => 0,
                'name' => '顶级权限',
            );
        }
        $this->assign(['menu_list' => json_encode($new_menus)]);
        $this->assign(['id' => $id]);
        return $this->fetch("checkbox_meun");
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}